package com.tjmntv.tj.myapplication.demo6

import android.content.ComponentName
import android.content.Intent
import android.content.ServiceConnection
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.IBinder
import androidx.lifecycle.Observer
import com.tjmntv.tj.myapplication.R
import kotlinx.android.synthetic.main.activity_kotlin_demo_aty6_2.*
import kotlinx.android.synthetic.main.aty_kotlin_demo_01.*

class KotlinDemoAty6_2 : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_kotlin_demo_aty6_2)

        buttonBindService2.setOnClickListener {
            val bindIntent = Intent(this, MyService::class.java)
            val serviceConnection = object : ServiceConnection {
                override fun onServiceConnected(name: ComponentName?, service: IBinder?) {
                    (service as MyService.MyBinder).service.numberLiveData.observe(this@KotlinDemoAty6_2, Observer {
                        textView3.text = "$it"
                    })
                }

                override fun onServiceDisconnected(name: ComponentName?) {
                    // TODO("Not yet implemented")
                }
            }
            bindService(bindIntent, serviceConnection, BIND_AUTO_CREATE)
        }
    }
}